package com.ruoyi.sycs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.sycs.domain.SycsDataDetail;
import com.ruoyi.sycs.mapper.SycsDataDetailMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.sycs.domain.bo.SycsDataBaseBo;
import com.ruoyi.sycs.domain.vo.SycsDataBaseVo;
import com.ruoyi.sycs.domain.SycsDataBase;
import com.ruoyi.sycs.mapper.SycsDataBaseMapper;
import com.ruoyi.sycs.service.ISycsDataBaseService;

import java.util.*;

/**
 * 试验数据设置主表Service业务层处理
 *
 * @author zyb
 * @date 2024-04-09
 */
@RequiredArgsConstructor
@Service
public class SycsDataBaseServiceImpl implements ISycsDataBaseService {

    private final SycsDataBaseMapper baseMapper;

    private final SysDictDataMapper dictDataMapper;

    private final SycsDataDetailMapper sycsDataDetailMapper;

    /**
     * 查询试验数据设置主表
     */
    @Override
    public SycsDataBaseVo queryById(Long bsId){
        return baseMapper.selectVoById(bsId);
    }

    /**
     * 查询试验数据设置主表列表
     */
    @Override
    public TableDataInfo<SycsDataBaseVo> queryPageList(SycsDataBaseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SycsDataBase> lqw = buildQueryWrapper(bo);
        Page<SycsDataBaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询试验数据设置主表列表
     */
    @Override
    public List<SycsDataBaseVo> queryList(SycsDataBaseBo bo) {
        LambdaQueryWrapper<SycsDataBase> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SycsDataBase> buildQueryWrapper(SycsDataBaseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SycsDataBase> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getBsName()), SycsDataBase::getBsName, bo.getBsName());
        lqw.eq(StringUtils.isNotBlank(bo.getSyDataProject()), SycsDataBase::getSyDataProject, bo.getSyDataProject());
        return lqw;
    }

    /**
     * 新增试验数据设置主表
     */
    @Override
    public Boolean insertByBo(SycsDataBaseBo bo) {
        SycsDataBase add = BeanUtil.toBean(bo, SycsDataBase.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBsId(add.getBsId());
        }
        return flag;
    }

    /**
     * 修改试验数据设置主表
     */
    @Override
    public Boolean updateByBo(SycsDataBaseBo bo) {
        SycsDataBase update = BeanUtil.toBean(bo, SycsDataBase.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SycsDataBase entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除试验数据设置主表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 查询试验数据设置树
     * @param bo
     * @return
     */
    @Override
    public List<Tree> treeList(SycsDataBaseBo bo) {
        // 返回值
        List<Tree> res = new ArrayList<>();
        // 获取字典值
        bo.setSyDataProject("sy_data_project");
        List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(bo.getSyDataProject());
        // 根节点
        Tree root = new Tree();
        root.setId(0);
        root.setName("试验数据设置项目");
        // 第一层 字典值
        List<Tree> ones = new ArrayList<>();
        for (SysDictData data : dictDatas) {
            Tree one = new Tree();
            one.setId(data.getDictValue());
            one.setName(data.getDictLabel());
            one.setParentId(0);
            ones.add(one);
            // 第二层 主表
            QueryWrapper<SycsDataBase> wrapper1 = new QueryWrapper();
            wrapper1.eq("sy_data_project", data.getDictValue());
            List<SycsDataBase> bases = baseMapper.selectList(wrapper1);
            List<Tree> twos = new ArrayList<>();
            for (SycsDataBase base : bases) {
                Tree two = new Tree();
                two.setId(base.getBsId());
                two.setParentId(data.getDictValue());
                two.setName(base.getBsName());
                // 第三层 明细表
                QueryWrapper<SycsDataDetail> wrapper2 = new QueryWrapper();
                wrapper2.eq("bs_id", base.getBsId());
                List<SycsDataDetail> details = sycsDataDetailMapper.selectList(wrapper2);
                List<Tree> threes = new ArrayList<>();
                for (SycsDataDetail detail : details) {
                    Tree three = new Tree();
                    three.setId(detail.getDeId());
                    three.setParentId(base.getBsId());
                    three.setChildren(new ArrayList<>());
                    threes.add(three);
                }
                two.setChildren(threes);
                twos.add(two);
            }
            one.setChildren(twos);
        }
        root.setChildren(ones);
        res.add(root);
        return res;
    }

}
